package com.soundShape.mediators.reporting
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.popUps.PhotoInfo;
	import com.soundShape.popUps.PhotoReportingInfo;
	import com.soundShape.utils.StringUtils;
	import com.soundShape.views.login.Login;
	import com.soundShape.views.reporting.Reporting;
	import com.soundShape.views.transactions.Transactions;
	import com.soundShape.vo.CustomerVO;
	import com.soundShape.vo.PhotoVO;
	import com.soundShape.vo.TreatmentVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Rotate;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import nz.co.codec.flexorm.criteria.Criteria;
	
	import spark.components.BorderContainer;
	
	
	
	
	public class ReportingMediator extends AbstractViewMediator
	{
		
		private var loader:Loader;
		private var bmp:Bitmap;
		private var img:Image;
		private var arrReportingImages:Array = [];
		private var collPhotoVOs:ArrayCollection;
		private var collTreatmentVOs:ArrayCollection;
		private var ndx:int = 0;
		
				
				public function ReportingMediator()
				{
					super(Reporting);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Reporting
				{
					return this._view as Reporting;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					
					BindingUtils.bindProperty(this.view.cmbxCustomers,"dataProvider",this.applicationModel.customers,"source");
					setCustomerComboBox();
					
					
					
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					//this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.cmbxCustomers.addEventListener(ListEvent.CHANGE, handleCmbxCustomersChange);
					
					this.view.lstImg.addEventListener(SoundShapeEvent.IMAGE_SELECTED, handleImageSelected);
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				private function handleCmbxCustomersChange(listEvent:ListEvent):void
				{
					
					if(collPhotoVOs != null)
					{
						collPhotoVOs.removeAll();
						collPhotoVOs.refresh();
						
					}
					
					if(ArrayCollection(this.view.lstImg.dataProvider) != null)
					{
						ArrayCollection(this.view.lstImg.dataProvider).removeAll();
						ArrayCollection(this.view.lstImg.dataProvider).refresh();
					}
					
					ndx = 0;
					
					this.applicationModel.customerVO = ComboBox(listEvent.currentTarget).selectedItem as CustomerVO;
					
					// get all photo's of client
					var criteria:Criteria = applicationModel.entityManager.createCriteria(PhotoVO);
					criteria.addEqualsCondition("customerID",this.applicationModel.customerVO.id);
					collPhotoVOs = this.applicationModel.entityManager.fetchCriteria(criteria);
					
					fileLoader();
				}
				
				private var _treatmentVO:TreatmentVO;
				
				private function fileLoader():void
				{
					
					if(this.collPhotoVOs.length == 0)
							return;
					
					var photoVO:PhotoVO = PhotoVO(this.collPhotoVOs.getItemAt(ndx));
					
					this.view.swfLdrProgress.visible = true;
					
					//get the treatment associated with this photo
					var criteria:Criteria = applicationModel.entityManager.createCriteria(TreatmentVO);
					criteria.addEqualsCondition("id",photoVO.treatmentID);
					
					var treatments:ArrayCollection = this.applicationModel.entityManager.fetchCriteria(criteria);
					this._treatmentVO = treatments.getItemAt(0) as TreatmentVO; // <- there can be only 1 treatment per photo
					
					var filePath:String = StringUtils.doSeparatorReplace(photoVO.path);
					var file:File = new File(filePath);
					
					loader = new Loader();
					var urlReq:URLRequest = new URLRequest(file.url);
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaded);
					loader.load(urlReq);
				}
				
				private function handleImageLoaded(event:Event):void
				{
					loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleImageLoaded);
					
					bmp = new Bitmap();
					bmp = loader.content as Bitmap;
					
					this.arrReportingImages.push({imgSource:bmp, treatmentData:_treatmentVO});
					
					ndx++;
					
					if(ndx < this.collPhotoVOs.length)
					{
						fileLoader();
					}
					else
					{
						this.view.lstImg.dataProvider = new ArrayCollection(this.arrReportingImages);
						this.view.swfLdrProgress.visible = false;
					}
				}
				
				protected function handleImageSelected(soundShapeEvent:SoundShapeEvent):void
				{
					var photoReportingInfo:PhotoReportingInfo = new PhotoReportingInfo();
					photoReportingInfo.applicationModel = this.applicationModel;
					photoReportingInfo.data = soundShapeEvent.data;
					PopUpManager.addPopUp(photoReportingInfo,this.view,true);
					PopUpManager.centerPopUp(photoReportingInfo);
				}
				
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				private function setCustomerComboBox():void
				{
					this.view.cmbxCustomers.selectedIndex = -1;
				}
				
			
		}
		
	}
